<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红绿灯案例</title>
    <style>
        #box img {
            width: 80px;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        var box = document.getElementById('box')
        function trafficlight() {
            this.color = 1;
            this.gu();
            this.rui();
        }
        trafficlight.prototype.gu = function () {
            this.dom = document.createElement('img');
            this.dom.src = 'images/' + this.color + '.jpg';
            box.appendChild(this.dom);
        }
        trafficlight.prototype.rui = function () {
            var slef = this;
            this.dom.onclick = function () {
                slef.xiang();
            }
        }
        trafficlight.prototype.xiang = function () {
           this.color++;
                if (this.color == 4) {
                    this.color = 1;
                }
              this.dom.src = 'images/' + this.color + '.jpg';
        }
    var grx=new trafficlight();
    new trafficlight();
    new trafficlight();
    new trafficlight();
    new trafficlight();
    new trafficlight();
   
    </script>
</body>

</html>